/**
 * Created by Edgard JR on 27/11/2014.
 */

/*SLIDE*/


$(function () {
    modalOpen = false;

    showFirstImage();
    setInterval(slide, 4000);

  /* $('.show-modal a').click(function () {
        showModal(this);
    });

    $('.overlay, .close').on('click', function () {
        closeModal();
    });

    $('.next').click(function () {
        nextPhoto();
    });


    $('.prev').click(function () {
        prevPhoto();
    });


    $(document).keyup(function (e) {
        const keyEsc = 27;
        const keySetaDireita = 39;
        const keySetaEsquerda = 37;

        if (modalOpen) {
            switch (e.keyCode) {
                case keyEsc:
                    closeModal();
                    break;
                case keySetaDireita:
                    nextPhoto();
                    break;
                case keySetaEsquerda:
                    prevPhoto();
                    break;
            }
        }
    });*/
});

function showModal(obj) {
    modalOpen = true;
    $(obj).addClass("selected");
    var srcImg = $(obj).attr("rel");
    var legenda = $(".selected").children().attr("data-description");

    //passa o caminho da imagem para a img do modal
    $('#imgModal').attr("src", srcImg);

    $(".legend").html(legenda);

    //pega o tamanho real da imagem
    var img = new Image();
    img.src = srcImg;

    if(img.width == 0 && img.height == 0){
        img.width = 720;
        img.height = 477;
    }

    resizeModal(img.width, img.height);

    $('.overlay, .modal').fadeTo(1000, 1.0);
}

function closeModal() {
    modalOpen = false;
    $(".selected").removeClass("selected");
    $('.overlay, .modal').hide();
}

function resizeModal(width, height) {
    //calculo para definir a margem do modal
    var marginTopModal = -parseInt(height / 2);
    var marginLeftModal = -parseInt(width / 2);

    // seta altura,largura e margens do modal;
    $('.modal').css({
            "width": width,
            "height": height,
            "margin-top": marginTopModal,
            "margin-left": marginLeftModal}
    );
}

function slide() {
    if ($(".ativo").next().size()) {
        $(".ativo").fadeOut().removeClass("ativo").next().fadeIn().addClass("ativo");
    } else {
        $(".ativo").fadeOut().removeClass("ativo");
        showFirstImage();
    }
}

function showFirstImage() {
    $("#slide img:eq(0)").addClass("ativo").show();
}

function nextPhoto() {
    if ($(".selected").next().size()) {

        var nextPhoto = $(".selected").next();
        var photoSelected = $(".selected");
        var legendNextPhoto = $(nextPhoto).children().attr("data-description");

        $('#imgModal').attr("src", $(nextPhoto).attr("rel"));
        $(photoSelected).removeClass("selected").next().addClass("selected");


        addLegendPhoto(legendNextPhoto);

        img = new Image();
        img.src = $('#imgModal').attr("src");

        resizeModal(img.width, img.height);
    }
}

function prevPhoto() {
    if ($(".selected").prev().size()) {
        var prevPhoto = $(".selected").prev();
        var photoSelected = $(".selected");
        var legendPrevPhoto = $(prevPhoto).children().attr("data-description");

        $('#imgModal').attr("src", $(prevPhoto).attr("rel"));
        $(photoSelected).removeClass("selected").prev().addClass("selected");


        addLegendPhoto(legendPrevPhoto);

        img = new Image();
        img.src = $('#imgModal').attr("src");

        resizeModal(img.width, img.height);
    }
}

function addLegendPhoto(legend) {
    $(".legend").html(legend);
}


